package com.fu.mr.bluetoothdchat;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.tencent.bugly.crashreport.CrashReport;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * <pre>
 *     author : Mr.Fu
 *     e-mail : 18622268981@163.com
 *     time   : 2020/05/07
 *     desc   : 自定义 Application 类
 *     version: 1.0
 * </pre>
 */

public class MyApplication extends Application implements Application.ActivityLifecycleCallbacks {

    private static final String TAG = MyApplication.class.getSimpleName();

    /**
     * 初始化资源
     * 数据共享
     * 数据缓存
     */
    @Override
    public void onCreate() {
        super.onCreate();

        Log.d(TAG, "onCreate -> App 初始化");

        // c7bf97d7a2
        Context context = getApplicationContext();
        // 获取当前包名
        String packageName = context.getPackageName();
        // 获取当前进程名
        String processName = getProcessName(android.os.Process.myPid());
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
        // 初始化Bugly
        CrashReport.initCrashReport(context, "c7bf97d7a2", true, strategy);
    }

    /**
     * 通知应用程序当前内存使用情况(以内存级别进行识别)
     *
     * @param level 当前内存不足级别
     */
    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);

        Log.d(TAG, "onTrimMemory -> 内存不足级别: " + level);
    }

    /**
     * 监听 Android 系统整体内存较低时刻
     */
    @Override
    public void onLowMemory() {
        super.onLowMemory();

        Log.d(TAG, "onLowMemory -> 监听 Android 系统整体内存较低时刻");
    }

    /**
     * 监听应用程序配置信息的改变，如屏幕旋转等
     *
     * @param newConfig 新的配置信息
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        Log.d(TAG, "onConfigurationChanged -> 新的配置信息: " + newConfig);
    }

    @Override
    public void registerActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
        super.registerActivityLifecycleCallbacks(callback);
    }

    @Override
    public void unregisterActivityLifecycleCallbacks(ActivityLifecycleCallbacks callback) {
        super.unregisterActivityLifecycleCallbacks(callback);
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {

        Log.d(TAG, "onActivityCreated -> " + activity.getLocalClassName());
    }

    @Override
    public void onActivityStarted(Activity activity) {
        Log.d(TAG, "onActivityStarted -> " + activity.getLocalClassName());
    }

    @Override
    public void onActivityResumed(Activity activity) {

        Log.d(TAG, "onActivityResumed -> " + activity.getLocalClassName());
    }

    @Override
    public void onActivityPaused(Activity activity) {

        Log.d(TAG, "onActivityPaused -> " + activity.getLocalClassName());
    }

    @Override
    public void onActivityStopped(Activity activity) {

        Log.d(TAG, "onActivityStopped -> " + activity.getLocalClassName());
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

        Log.d(TAG, "onActivitySaveInstanceState -> " + activity.getLocalClassName());
    }

    @Override
    public void onActivityDestroyed(Activity activity) {

        Log.d(TAG, "onActivityDestroyed -> " + activity.getLocalClassName());
    }

    /**
     * 获取进程号对应的进程名
     *
     * @param pid
     * @return
     */
    private String getProcessName(int pid) {

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
            String processName = reader.readLine();
            if (!TextUtils.isEmpty(processName)) {
                processName = processName.trim();
            }
            return processName;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }
        return null;
    }
}
